<?php

namespace post\modules\user\controllers;

use post\modules\user\models\User;
use Yii;

use yii\web\Controller;
use post\base\BaseController;

class LoginController extends BaseController
{


    public function actionIndex()
    {


        $request = Yii::$app->request;

        if ($request->isPost) {
            $request_arr = ['user_email', 'user_pwd'];
            $data =data_validate($request_arr);
            if ($data == "false") {
                return $this->json_error("参数缺失");
            }
            $userModel = $this->actionFindUser($data['user_email']);


            if ($userModel == null) {
                return $this->json_error("没有这个用户!");
            }

            $user = $userModel->toArray();
            $password = crypt($data['user_pwd'], $user['salt']);
            if ($password == $user['password']) {

                $arr['username']=$user['username'];
                $arr['access_token']=$user['access_token'];
                $userInfo=$userModel->info;
                $tou=$userInfo->toArray()['tou'];
                $arr['tou']=$tou;

                return $this->json_ok($arr);
            } else {
                return $this->json_error("登陆失败ERROR!");
            }

        }
    }


    public function actionLoginInfo()
    {
        $request = Yii::$app->request;
        if ($request->isPost) {
            $session = Yii::$app->session;

            $data = $session->get("user");

//                $this->dd($session);
            if ($data != null) {
                return $this->json_ok($data);
            } else {
                return $this->json_error("未登录");
            }

        }else{

            $session = Yii::$app->session;
$session->set("user","123123");
            $data = $session->get("user");
            $this->dd($data);
            
            $cookieID = new \yii\web\Cookie();
            $cookieID -> name = 'wl_php';        //cookie的名称
            $cookieID -> expire = time() + 3600;	   //存活的时间
            $cookieID -> httpOnly = true;		   //无法通过js读取cookie
            $cookieID -> value = 3;	   //cookie的值

            \Yii::$app->response->getCookies()->add($cookieID);
//            \Yii::$app->response->getCookies()->add($cookie);

            $cookie = \Yii::$app->request->cookies;
            $value= $cookie->getValue('token');

            $this->dd($value);

        }
    }

//查找用户
    public function actionFindUser($email = null)
    {
        $user = null;
        if ($email != null) {
            $user = User::find()->where(['email' => $email])->one();


        } else {
            $request_arr = ['user_email'];
            $data = $this->data_validate($request_arr);
            if ($data == "false") {
                return $this->json_error("参数缺失");
            }
            $user = User::find()->where(['email' => $data['user_email']])->one();

        }

        if ($user != null) {
            return $user;
        } else {
            return null;
        }


    }


}
